﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.IO;

namespace simWaifu
{
    public class Mood
    {
        // todo: move rand to some global static helper class
        static Random rand = new Random();

        string name;
        Waifu.MoodType type;
        List<string> messages;
        List<string> pictures;
        List<string> sounds;
        int curMsg;
        int curPic;
        int curSnd;
        
        public Mood(Waifu.MoodType t)
        {
            type = t;
            name = "";
            messages = new List<string>();
            pictures = new List<string>();
            sounds = new List<string>();
        }

        public void Clear()
        {
            messages.Clear();
            pictures.Clear();
            sounds.Clear();
        }

        public void SetName(string m)
        {
            name = m;
        }

        public string GetName()
        {
            return name;
        }

        public void AddMessage(string m)
        {
            messages.Add(m);
        }

        public void AddSound(string s)
        {
            sounds.Add(s);
        }

        public void AddPicture(string p)
        {
            pictures.Add(p);
        }

        public string ChangePicture()
        {
            string fileName = "";

            if (pictures.Count > 0)
            {
                int r = rand.Next(this.pictures.Count);
                if (pictures.Count > 1)
                    while (r == curPic)
                        r = rand.Next(this.pictures.Count);

                string waifuPath = Waifu.GetWaifuFolder();
                bool bFound = false;
                do
                {
                    fileName = Path.Combine(waifuPath, this.name, this.pictures.ElementAt(r));
                    if (System.IO.File.Exists(fileName))
                    {
                        bFound = true;
                        curPic = r;
                    }
                    else
                    {
                        DebugMessageHandler.WriteMsgLine("Mood image file deleted, removing it from the list: "
                            + fileName);
                        pictures.RemoveAt(r);
                        fileName = "";
                        r--;
                    }
                } while (!bFound && pictures.Count > 0);
            }
            return fileName;
        }

        public string ChangeSound()
        {
            string fileName = "";

            if (sounds.Count > 0)
            {
                int r = rand.Next(this.sounds.Count);
                if (sounds.Count > 1)
                    while (r == curPic)
                        r = rand.Next(this.sounds.Count);

                string waifuPath = Waifu.GetWaifuFolder();
                bool bFound = false;
                do
                {
                    fileName = Path.Combine(waifuPath, this.name, this.sounds.ElementAt(r));
                    if (System.IO.File.Exists(fileName))
                    {
                        bFound = true;
                        curSnd = r;
                    }
                    else
                    {
                        DebugMessageHandler.WriteMsgLine("Mood sound file deleted, removing it from the list: "
                            + fileName);
                        sounds.RemoveAt(r);
                        fileName = "";
                        curSnd--;
                    }
                } while (!bFound && sounds.Count > 0);
            }
            return fileName;
        }

        public string ChangeMessage()
        {
            string msg = "";
            if (messages.Count > 0)
            {
                int m = rand.Next(messages.Count);
                if (messages.Count > 1)
                {
                    while (m == curMsg)
                        m = rand.Next(messages.Count);
                }
                curMsg = m;
                msg = messages.ElementAt(m);
            }
            return msg;
        }
    }
}

